package com.example.myandroidtest.ui.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

public class CustomLayout extends ViewGroup {
    public CustomLayout(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // 获取宽度和高度的测量模式和尺寸
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int childWidth = (int) (100 * getResources().getDisplayMetrics().density);
        int childHeight = (int) (100 * getResources().getDisplayMetrics().density);
        int horizontalSpacing = (int) (20 * getResources().getDisplayMetrics().density);
        int verticalSpacing = (int) (20 * getResources().getDisplayMetrics().density);

        int width = 0;    //	ViewGroup的宽
        int height = 0;    //	ViewGroup的高
        int rowWidth = 0;    //当前行的宽
        int rowHeight = childHeight;    //当前行的高

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            // 测量子View的大小
            measureChild(child, MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY));
            // 更新当前行的宽度
            rowWidth += childWidth + horizontalSpacing;        // rowWidth = childWidth +
            // horizontalSpacing + rowWidth
            // 检查当前行宽度是否超过ViewGroup的宽度
            if (rowWidth > widthSize) {
                // 更新ViewGroup的宽度
                width = Math.max(width, rowWidth - horizontalSpacing);
                // 累加高度
                height += rowHeight + verticalSpacing;        // height = rowHeight +
                // verticalSpacing + height
                // 重置当前行的宽度
                rowWidth = childWidth + horizontalSpacing;
            }
        }
        // 更新ViewGroup的宽度和高度
        width = Math.max(width, rowWidth - horizontalSpacing);
        height += rowHeight;    // height = rowHeight + height
        // 设置ViewGroup的测量宽高
        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : width,
                heightMode == MeasureSpec.EXACTLY ? heightSize : height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getWidth();
        int childWidth = (int) (100 * getResources().getDisplayMetrics().density);
        int childHeight = (int) (100 * getResources().getDisplayMetrics().density);
        int horizontalSpacing = (int) (20 * getResources().getDisplayMetrics().density);
        int verticalSpacing = (int) (20 * getResources().getDisplayMetrics().density);
        int x = 0;
        int y = 0;

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);

            if (x + childWidth > width) {
                x = 0;
                y += childHeight + verticalSpacing;
            }

            child.layout(x, y, x + childWidth, y + childHeight);
            x += childWidth + horizontalSpacing;    // x = childWidth + horizontalSpacing + x
        }
    }

}
